#pragma once

#include <assert.h>
#include <string>
#include <memory>
#include <vector>
#include <functional>

#include "zrpc/net/channel.h"
#include "zrpc/net/event_loop.h"
#include "zrpc/net/socket.h"

namespace zrpc {
class Acceptor {
 public:
  typedef std::function<void(int32_t)> NewConnectionCallback;

  Acceptor(EventLoop *loop, const std::string &ip, int16_t port);

  ~Acceptor();

  void SetNewConnectionCallback(const NewConnectionCallback &&cb) {
    new_connection_callback_ = std::move(cb);
  }

  bool Getlistenning() const { return listenning_; }

  void Listen();

  void HandleRead();

 private:
  Acceptor(const Acceptor &);

  void operator=(const Acceptor &);

  EventLoop *loop_;
  Channel channel_;
  int32_t sockfd_;

  NewConnectionCallback new_connection_callback_;
  bool listenning_;
  bool idlefd_;
};

}  // namespace zrpc